<template>
  <div>
    <el-form ref="form" :model="user" :rules="rules" label-width="120px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="user.nickName" maxlength="30" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="user.phonenumber" maxlength="11" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="user.email" maxlength="50" />
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="user.sex">
          <el-radio label="0">男</el-radio>
          <el-radio label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="submit">保存</el-button>
        <el-button type="danger" size="mini" @click="close">关闭</el-button>
      </el-form-item>

      <el-form-item label="推荐链接" v-if="userType == 'seller'">
        <el-link :underline="false" @click="copyUrl" type="success">
          {{ recommenUrl }} <i class="el-icon-document-copy"></i>
        </el-link>
      </el-form-item>
      <el-form-item class="auth" v-for="item in authUser">
        <template #label>
          <el-image style="width: 30px; height: 30px" fit="contain" :src="item.icon" />
        </template>
        <div class="auth-wrapper">
          <div class="auth-wrapper-item" v-if="item.isBind == 0" @click="handleBinding(item)" data>绑定</div>
          <div class="auth-wrapper-item" v-else>
            <div style="font-weight: bold">{{ item.nickName }}</div>
            <el-link style="margin-left: 10px" type="primary" @click="handleUnbind(item)">解绑</el-link>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateUserProfile, oauthRender, unBind } from '@/api/system/user'
import useCopy from '@/utils/useCopy'
const distrId = JSON.parse(localStorage.getItem('distr') || '{}').distrId
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  components: {},
  props: {
    user: {
      type: Object,
    },
    authUser: {
      type: Array,
    },
  },
  data() {
    return {
      userType: this.$store.state.user.userType,
      /**推荐链接 */
      recommenUrl: `${location.origin}/platform/recommenSellerRegist?distrId=${distrId}`,
      // 表单校验
      rules: {
        nickName: [{ required: true, message: '用户昵称不能为空', trigger: 'blur' }],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change'],
          },
        ],
        phonenumber: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          // {
          //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          //   message: "请输入正确的手机号码",
          //   trigger: "blur",
          // },
        ],
      },
    }
  },
  mounted() {},
  methods: {
    handleBinding(data) {
      oauthRender(data.source).then((res) => {
        location.href = res
      })
    },
    handleUnbind(data) {
      this.$confirm(this.$t('确认解除绑定?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        unBind(data.source).then((res) => {
          const { code, msg } = res
          if (code === 200) {
            this.$modal.msgSuccess(msg)
            this.$emit('refresh')
          }
        })
      })
    },
    copyUrl() {
      useCopy(this.recommenUrl)
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          updateUserProfile(this.user).then((response) => {
            this.$modal.msgSuccess('修改成功')
          })
        }
      })
    },
    close() {
      this.$tab.closePage()
    },
  },
}
</script>

<style lang="scss" scoped>
.auth {
  display: flex;
  cursor: pointer;
  ::v-deep .el-form-item__content {
    margin-left: 0 !important;
    flex: 1;
  }
  &-wrapper {
    display: flex;
    &-item {
      display: flex;
      width: 100%;
      align-items: center;
    }
  }
}
</style>
